from fastapi import FastAPI, HTTPException
import uvicorn
from pydantic import BaseModel
from math import gcd
from typing import Tuple

app = FastAPI(title="GCD和LCM计算服务API")

class NumbersInput(BaseModel):
    a: int
    b: int

def calculate_gcd_lcm(a: int, b: int) -> Tuple[int, int]:
    """计算GCD和LCM"""
    if a == 0 or b == 0:
        return 0, 0
    gcd_val = gcd(a, b)
    lcm_val = abs(a * b) // gcd_val
    return gcd_val, lcm_val

@app.post("/gcd_lcm")
async def compute_gcd_lcm(input: NumbersInput):
    """计算最大公约数和最小公倍数"""
    if input.a < 0 or input.b < 0:
        raise HTTPException(status_code=400, detail="请输入正整数")
    
    gcd_val, lcm_val = calculate_gcd_lcm(input.a, input.b)
    
    return {
        "numbers": (input.a, input.b),
        "gcd": gcd_val,
        "lcm": lcm_val,
        "message": f"GCD是{gcd_val}, LCM是{lcm_val}"
    }

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8001)